package com.cysoft.domain;

import java.time.Duration;
import java.time.LocalDateTime;

public class MoldWorkState {
    private long moldId;

    private Mold mold;

    private LocalDateTime beginWorkTime;
    private LocalDateTime endWorkTime;
    private LocalDateTime nextWorkTime;

    public MoldWorkState() {
    }

    public MoldWorkState(long moldId, Mold mold) {
        this.moldId = moldId;
        this.mold = mold;
    }

    public long getMoldId() {
        return moldId;
    }

    public void setMoldId(long moldId) {
        this.moldId = moldId;
    }

    public Mold getMold() {
        return mold;
    }

    public void setMold(Mold mold) {
        this.mold = mold;
    }

    public LocalDateTime getBeginWorkTime() {
        return beginWorkTime;
    }

    public void setBeginWorkTime(LocalDateTime beginWorkTime) {
        this.beginWorkTime = beginWorkTime;

        if(this.endWorkTime == null || beginWorkTime == null) {
            return;
        }
        Duration duration = Duration.between(this.beginWorkTime,this.endWorkTime);
        if(duration.getSeconds() > Constant.MOLD_CONTINUE_WORK_TIME*60) {
            this.endWorkTime.plusMinutes(Constant.MOLD_CONTINUE_SLEEP_TIME);
//            System.out.println("工作时间超时，需要休息一会儿");
        }
    }

    public LocalDateTime getEndWorkTime() {
        return this.endWorkTime;
    }

    public void setEndWorkTime(LocalDateTime endWorkTime) {
        this.endWorkTime = endWorkTime;
        if(this.beginWorkTime == null || endWorkTime == null) {
            return;
        }
        Duration duration = Duration.between(this.beginWorkTime,endWorkTime);
        if(duration.getSeconds() > Constant.MOLD_CONTINUE_WORK_TIME*60) {
            this.endWorkTime.plusMinutes(Constant.MOLD_CONTINUE_SLEEP_TIME);
//            System.out.println("工作时间超时，需要休息一会儿");
        }
    }

    public void resetState() {
        this.beginWorkTime = null;
        this.endWorkTime = null;
        this.nextWorkTime = null;
    }
    public LocalDateTime getNextWorkTime() {
        return nextWorkTime;
    }

    public void setNextWorkTime(LocalDateTime nextWorkTime) {
        this.nextWorkTime = nextWorkTime;
    }

    public boolean checkValid(LocalDateTime beginWorkTime,LocalDateTime endWorkTime) {
        if(this.beginWorkTime==null || this.endWorkTime==null) {
            return true;
        }
        if(beginWorkTime!=null  &&  beginWorkTime.compareTo(this.beginWorkTime) > 0 && beginWorkTime.compareTo(this.endWorkTime) < 0) {
            return false;
        }
        if(endWorkTime!=null &&  endWorkTime.compareTo(this.beginWorkTime) > 0 && endWorkTime.compareTo(this.endWorkTime) < 0) {
            return false;
        }
        return true;
    }
}
